﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace tdgame.Lighting
{
    class DirectionalLight : Light
    {
        private static Effect lightEffect = null;
        public Vector3 Direction { get; set; }
        public Color Color { get; set; }
        public float Intensity { get; set; }


        public DirectionalLight(Game game)
        {
            if (DirectionalLight.lightEffect == null)
                DirectionalLight.lightEffect = game.Content.Load<Effect>("DirectionalLight");
        }

        public override void Render(QuadRenderer renderer, Texture2D colorMap, Texture2D normalMap, Texture2D depthMap, ref Matrix view, ref Matrix projection, ref Vector3 cameraPosition, ref Vector2 halfPixel)
        {
            Vector2 min = Vector2.One * -1;
            Vector2 max = Vector2.One;
            DirectionalLight.lightEffect.Parameters["lightDirection"].SetValue(this.Direction);
            DirectionalLight.lightEffect.Parameters["Color"].SetValue(this.Color.ToVector3());
            DirectionalLight.lightEffect.Parameters["cameraPosition"].SetValue(cameraPosition);
            DirectionalLight.lightEffect.Parameters["InvertViewProjection"].SetValue(Matrix.Invert(view * projection));
            DirectionalLight.lightEffect.Parameters["colorMap"].SetValue(colorMap);
            DirectionalLight.lightEffect.Parameters["normalMap"].SetValue(normalMap);
            DirectionalLight.lightEffect.Parameters["depthMap"].SetValue(depthMap);
            DirectionalLight.lightEffect.Parameters["intensity"].SetValue(this.Intensity);
            DirectionalLight.lightEffect.Parameters["halfPixel"].SetValue(halfPixel);
            DirectionalLight.lightEffect.Techniques[0].Passes[0].Apply();
            renderer.Draw(ref min, ref max);
        }
    }
}
